package com.nf.health.app.customview;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.core.DialogOnitem;
import com.nf.health.app.customview.PickerView.onSelectListener;

import java.util.List;

/**
 * 底部弹出框对话框
 * 
 * @author Ming
 * 
 */
public class BottomSslideDialog extends Dialog implements
		android.view.View.OnClickListener {
	private Context context;
	private DialogOnitem OnItemClickListener;
	private String[] content;
	private List<String> listArray;
	private Dialog customDialog;
	private int position = -1;
	// content 第一个为标题 如果数组第一个值为空 不显示title
	public BottomSslideDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener, String[] content,
			DialogOnitem onItemClickListener) {
		super(context, cancelable, cancelListener);
		this.context = context;
		this.OnItemClickListener = onItemClickListener;
		this.content = content;
	}
	// content 第一个为标题 如果数组第一个值为空 不显示title
	public BottomSslideDialog(Context context, int currentPos, List<String> listArray,
			DialogOnitem onItemClickListener) {
		super(context);
		this.position = currentPos;
		this.context = context;
		this.OnItemClickListener = onItemClickListener;
		this.listArray = listArray;
	}
	

	public BottomSslideDialog(Context context,
			DialogOnitem onItemClickListener) {
		super(context);
		this.context = context;
		this.OnItemClickListener = onItemClickListener;
	}
	public void createPicker(){
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// instantiate the dialog with the custom Theme
		View layout = inflater.inflate(R.layout.pop_picker, null);
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;

		customDialog = new Dialog(context, R.style.Dialog);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		customDialog.setContentView(layout, layoutParams);
		customDialog.setCanceledOnTouchOutside(true);
		Window window = customDialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.85f;
		window.setAttributes(lp);

		window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
		window.setWindowAnimations(R.style.mystyle); // 添加动画
		
		View btn_ok = layout.findViewById(R.id.btn_ok);
		btn_ok.setTag(0);
		btn_ok.setOnClickListener(this);
		PickerView pv_card = (PickerView) layout.findViewById(R.id.pv_card);
		pv_card.setData(listArray);
		pv_card.setSelected(position);
		pv_card.setOnSelectListener(new onSelectListener() {

			@Override
			public void onSelect(String text, int position) {
				BottomSslideDialog.this.position = position;
			}
		});
		View btn_cancel = layout.findViewById(R.id.btn_cancel);
		btn_cancel.setTag(100);
		btn_cancel.setOnClickListener(this);

		customDialog.show();
	}
	
	public void createInqueryDialog(){

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// instantiate the dialog with the custom Theme
		View layout = inflater.inflate(R.layout.pop_ask_question, null);
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;

		customDialog = new Dialog(context, R.style.Dialog);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		customDialog.setContentView(layout, layoutParams);
		customDialog.setCanceledOnTouchOutside(true);
		Window window = customDialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.85f;
		window.setAttributes(lp);

		window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
		window.setWindowAnimations(R.style.mystyle); // 添加动画
		
		View tv_ask_doctor = window.findViewById(R.id.tv_ask_doctor);
		tv_ask_doctor.setTag(0);
		tv_ask_doctor.setOnClickListener(this);
		View tv_ask_public = window.findViewById(R.id.tv_ask_public);
		tv_ask_public.setTag(1);
		tv_ask_public.setOnClickListener(this);
		View iv_del = window.findViewById(R.id.iv_del);
		iv_del.setTag(100);
		iv_del.setOnClickListener(this);

		customDialog.show();
	}
	

	/**
	 * Create the custom dialog
	 */
	public void create() {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// instantiate the dialog with the custom Theme
		View layout = inflater.inflate(R.layout.bottom_dialog, null);
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;

		customDialog = new Dialog(context, R.style.Dialog);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		customDialog.setContentView(layout, layoutParams);
		customDialog.setCanceledOnTouchOutside(true);
		Window window = customDialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.85f;
		window.setAttributes(lp);

		window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
		window.setWindowAnimations(R.style.mystyle); // 添加动画

		LinearLayout parentView = (LinearLayout) window
				.findViewById(R.id.parentView);
		window.findViewById(R.id.cancel_textview).setOnClickListener(this);

		try {
			TextView title_textview = (TextView) window
					.findViewById(R.id.title_textview);
			title_textview.setTextColor(Color.parseColor("#c2c0c0"));
			title_textview.setText(content[0]);
			if (content[0].equals("")) {
				window.findViewById(R.id.dialog_line).setVisibility(View.GONE);
				title_textview.setVisibility(View.GONE);
			}

			int size = content.length;
			for (int i = 1; i < size; i++) {
				View dialog_text_item = inflater.inflate(
						R.layout.dialog_text_item, null);
				TextView textView = (TextView) dialog_text_item
						.findViewById(R.id.dialog_textview);
				textView.setText(content[i]);
				parentView.addView(dialog_text_item);

				if (i == size - 1) {
					dialog_text_item.findViewById(R.id.dialog_line)
							.setVisibility(View.GONE);
				}
				dialog_text_item.setTag(i - 1);
				dialog_text_item.setOnClickListener(this);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		customDialog.show();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int position = view.getTag() == null ? 100 : (Integer) view.getTag();
		if (position == 100) {
			customDialog.dismiss();
		} else {
			if (OnItemClickListener != null)
				if(this.position == -1){
					OnItemClickListener.onItemClickListener(position);
				}else{
					OnItemClickListener.onItemClickListener(this.position);
				}
			customDialog.dismiss();
		}
	}
}
